<template>
  <div class="dialog-container">
    <el-dialog
      v-dialogDrag
      title="资源忽略信息"
      :visible.sync="dialogFormVisible"
      @open="onOpen"
      @opened="onOpened"
      @close="onClose"
      @closed="onClosed"
    >
      <div class="form-container">
        <el-form ref="form" :model="form" :rules="rules" label-position="top">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="描述" prop="description">
                <el-input v-model="form.description" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="顺序" prop="sort">
                <el-input v-model="form.sort" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="类型">
                <el-radio-group v-model="form.type" @change="void 0">
                  <el-radio :label="1">全局</el-radio>
                  <el-radio :label="2">资源服务</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" maxlength="255" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // ----------------------弹框部分-------------------------
      loading: false,
      dialogFormVisible: false,
      form: {
        id: 0,
        name: '',
        description: '',
        sort: 0,
        type: 1,
        remark: ''
      },
      rules: {
        name: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        description: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        }
      }
    }
  },
  created() {},
  methods: {
    onOpen() {
      // 重置表单内容
      this.resetForm()
      // 重置表单验证
      this.resetFields('form')
      console.log(this.type, this.rowData)
    },
    resetFields(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields()
      })
    },
    resetForm() {
      this.$nextTick(() => {
        this.form = {
          id: 0,
          name: '',
          description: '',
          sort: 0,
          type: 1,
          remark: ''
        }
      })
    },
    onOpened() {
      if (this.type === 'add') {
        // 无动作
      } else {
        if (this.rowData && this.rowData.id) {
          for (var key in this.form) {
            this.form[key] = this.rowData[key]
          }
        }
      }
    },
    onClose() {},
    onClosed() {
      this.$parent.rowData = {}
    },
    onSubmit() {
      const formName = 'form'
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveFormdata()
        }
      })
    },
    saveFormdata() {
      let url
      if (!this.form.id) {
        url = 'system/resourceIgnore/add'
      } else {
        url = 'system/resourceIgnore/edit'
      }
      this.$store.dispatch(url, this.form).then((data) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.$parent.onSearch()
        })
    }
  }
}
</script>
